const Card = require("./Card");
const CardBox = require("./CardBox");
const Table = require("./Table");
const Player = require("./Player");
const PLAYER_STATUS = Player.STATUS;
const PlayerList = Player.PlayerList;
const EventEmitter = require('events');
const rule = require("./rule");

const PLAYER_LIST = new PlayerList("player_1",
	"player_2",
	"player_3",
	"player_4"
).map(username => new Player(username, new EventEmitter()));

const TABLE = new Table();
const CARDBOX = new CardBox();

function* gameGenerator(command) {
	// 发牌
	console.log("开始发牌");

	CARDBOX.moveCardsToPlayers(PLAYER_LIST);

	const PLAYER_MAP = new Map();
	PLAYER_LIST.forEach(player => {
		PLAYER_MAP.set(player.username, player);
		player.sortCards();
		console.log(player.username, player.card_list.toString())
	});


	console.log("-----牌局正式开始-----")

	console.log(command);

	const results = yield PLAYER_LIST;

	const user_score_map = new Map();
	/* 计分 */
	// 先检查牌是否作弊
	results.forEach(result => {
		const username = result.username;
		const player = PLAYER_MAP.get(username);
		if (player) {
			const sou_card_list = player.card_list.slice().sort_cards();
			const res_card_list = result.card_list.slice().sort_cards();
			if (sou_card_list.some((card, i) => res_card_list[i] !== card)) {
				throw new Error("牌面信息不符合，有作弊情况！");
			}
		}
	});

}
